package com.shenbianyun.ycx;

import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.pay.PaymentReqModel;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：张一凡
 * @date ：2023年11月01日 18:37:12
 * @description：易畅行生产下发
 */
public class YCXTestPaymentServiceJOlineALIJK extends AbstractJavaSamplerClient {



    public final String CHARSET_UTF_8 = "UTF-8";
    public final Long MER_ID = 1697534870141137L;
    public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANI9mMMSLm+Baqw2CwTTytpSFOwwHX/udkgyy0agQ5Tgw1GEIeelYlTUg8Qsl1mYK6ftpoV2pUXsPiDmrSQ5gxPs6q6axwzbvGzKqTL+sU+nHdWUnlxolJN4Xfzyr+hzFdHkFRmAL72wbP/Hnpoeo/BiJLDJraZHTqiQsYvQVK3xAgMBAAECgYAE0yUOVPx70pyAFz1pfvLIz9lrEi+bH/3W6lN/622hnQbeeAnPJxx6leXwOpW+mJ9n2yxXI8HnOOS0HJccnbVEVSR5M4KeB08t7G6+ADY42DBAn4//LFTjfqbgvG+X2ECbWBALWbXgMS66v1L22RpnmJGHJsV3vfNrkkFLtAwjgQJBAO7Nxwzr1CxZ2kZyNs1e3s3bdKwk2H2NKC4CktjJF7aIVu0ck9m/DDooCCy/wUQ72fKGz5qmFI58lC1vRR2hoOcCQQDhYUWDrKlg4a5/VePcLX68ehH1/HQz3pj8VhJjYKPTn+6Yb8VZ+ea6ib6+qg1EZ/f49ROEB9KAc9z0IfFc8GdnAkEAxPCv19glgu4XNUG8MGKtHNh/v/gjEmf3WvqEYIR3KfIGvkVzKYCFdPDOzRjXKo6xj3HehIuzKE9VZKL/PRCuwQJBAJvbkZ/qYkOOLgb+1ZFooNEkNZF/oLhSAkfWGedBaGmrmustuDNtvuJW6WKe5IfewjwW9wUCB88u0Ki/ZFNaKJcCQAmyID6pNpPKv9NpIYVXFr0Nvh3nbPd8p5Hyxqtw+qGTa64MiHdWCHV5C5+DXa29wtgUDce6d5OiOTNC9KA8dbY=";
    public final String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDSPZjDEi5vgWqsNgsE08raUhTsMB1/7nZIMstGoEOU4MNRhCHnpWJU1IPELJdZmCun7aaFdqVF7D4g5q0kOYMT7OqumscM27xsyqky/rFPpx3VlJ5caJSTeF388q/ocxXR5BUZgC+9sGz/x56aHqPwYiSwya2mR06okLGL0FSt8QIDAQAB";
    public final String INTER_KEY = "F44D36620515AA071476355A3E42A192";
    public final String API_VERSION = "V1.0";
    public final String OMS_TEST_URL = "http://gateway.ycxlg.com/ycx/clientapi/clientBusiness/common";
    public final Long PROJECT_ID = 19L;


    public RequestMessage rm;
    public PaymentReqModel paymentReqModel;
    public PaymentReqModel.PayItems payItems;
    public List payItemsList;


    public String runTestPaymentServiceJ() throws Exception {

        rm = new RequestMessage();
        //生成guid码 理论上不重复
//        String ReqIdrandomGUID = new RandomGUID().toString();
//        rm.setReqId("test" + ReqIdrandomGUID);
        rm.setReqId("OnlineAPI" + System.currentTimeMillis() + 1);
        rm.setFunCode("6001");
        rm.setMerId(MER_ID);
        rm.setVersion(API_VERSION);


        /**--------————————————业务请求参数reqData——————————————————**/
        paymentReqModel = new PaymentReqModel();
        //生成guid码 理论上不重复
//        String BatchIdrandomGUID = new RandomGUID().toString();
        //商户批次号
//        paymentReqModel.setMerBatchId(BatchIdrandomGUID.substring(0, 30));
        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
        paymentReqModel.setMerBatchId(String.valueOf(id));
        //服务商id
        paymentReqModel.setProviderId(35304L);
        //任务id（项目id）
        paymentReqModel.setTaskId(PROJECT_ID);
        //批量付款payItems字段
        payItemsList = new ArrayList();
        payItems = new PaymentReqModel.PayItems();
        //收款人名称
        payItems.setPayeeName("崔方方");
        //身份证号
        payItems.setIdCard("412828199310044519");
        //收款人账号
        payItems.setPayeeAcc("6214830102464327");
        //金额（分）
        payItems.setAmt(1L);
        //收款人手机号
        payItems.setMobile("18001150364");
        //备注
        payItems.setMemo("api请求ycx");
        //商户订单号，同批次内确保唯一不可重复。
        payItems.setMerOrderId("order" + id);
        //付款方式 0：银行卡，1：支付宝，2：微信
        payItems.setPaymentType(0);
        /**异步通知地址**/
        //payItems.setNotifyUrl("http://192.168.40.78:8081/pay/notify");

        payItemsList.add(payItems);
        paymentReqModel.setPayItems(payItemsList);

        String json = JsonUtils.toJson(paymentReqModel);
//        System.out.println("请求未加密数据:" + json);

        //加密处理
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));

        String reqStr = JsonUtils.toJson(rm);
//        System.out.println("请求数据：" + reqStr);

        //发送请求
        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
//        System.out.println("响应数据：" + resData);


        return resData;
    }


    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            resData = runTestPaymentServiceJ();

            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("resCode").equals("0000")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + "", SampleResult.TEXT);
                System.out.println("批量付款结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("批量付款结果:" + false + " " + jsonObject1.get("resMsg"));
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }

}
